import { Button, Input, Form } from "@nutui/nutui-react-taro";
import { HouseEntity } from "@/entities/HouseEntity";
import { forwardRef, useImperativeHandle } from "react";
import { View, Text } from "@tarojs/components";
import { RentBillEntity } from "@/entities/RentBillEntity";
interface IProps {
  isFirstMonthBill: boolean;
  isSelectBillTime: boolean;
  currentHouse: HouseEntity.IBaseHouse;
  isDisabledForm: boolean;
  selectedMonthBill: RentBillEntity.IRentBill;
  onFinish?: (values) => any;
  onLookBill?: () => any;
}
const BillForm = (props: IProps, ref) => {
  const {
    isFirstMonthBill,
    isSelectBillTime,
    currentHouse,
    isDisabledForm,
    selectedMonthBill,
    onFinish,
    onLookBill,
  } = props;
  const [form] = Form.useForm();
  useImperativeHandle(
    ref,
    () => {
      return {
        setFieldsValue: (values: any) => {
          form.setFieldsValue(values);
        },
        resetFields: () => {
          form.resetFields();
        },
      };
    },
    [form]
  );
  return (
    <Form
      form={form}
      labelPosition="left"
      footer={
        isDisabledForm ? (
          <View className="bill-result-wrap">
            <View className="price">
              <Text>￥</Text>
              <Text>{selectedMonthBill.totalPrice}元</Text>
            </View>
            <View className="tip">
              <Text>当前选择的月份租金已收取，</Text>
              <Text className="entry-text" onClick={onLookBill}>
                {"查看详情 >"}
              </Text>
            </View>
          </View>
        ) : (
          <Button
            className="confirm-bill-btn"
            nativeType="submit"
            type="primary"
          >
            确定收租
          </Button>
        )
      }
      onFinish={onFinish}
      disabled={isDisabledForm}
    >
      {/* 电表 */}
      {isSelectBillTime && (
        <Form.Item
          label="上月份电表记录值："
          rules={[
            {
              required: true,
              message: "必填项",
            },
          ]}
          name="lastElectricityMeter"
        >
          <Input
            placeholder="请输入电表记录值"
            type="number"
            disabled={!isFirstMonthBill}
          ></Input>
        </Form.Item>
      )}
      <Form.Item
        label="当前月份电表记录值："
        rules={[
          {
            required: true,
            message: "必填项",
          },
        ]}
        name="electricityMeter"
      >
        <Input placeholder="请输入电表记录值" type="number"></Input>
      </Form.Item>
      {/* 水表 */}
      {isSelectBillTime && (
        <Form.Item
          label="上月份水表记录值："
          rules={[
            {
              required: true,
              message: "必填项",
            },
          ]}
          name="lastWaterMeter"
        >
          <Input
            placeholder="请输入水表记录值"
            type="number"
            disabled={!isFirstMonthBill}
          ></Input>
        </Form.Item>
      )}
      <Form.Item
        label="当前月份水表记录值："
        rules={[
          {
            required: true,
            message: "必填项",
          },
        ]}
        name="waterMeter"
      >
        <Input placeholder="请输入水表记录值" type="number"></Input>
      </Form.Item>
      {currentHouse.fuelFee > 0 && (
        <>
          {isSelectBillTime && (
            <Form.Item
              label="上月份燃气表记录值："
              rules={[
                {
                  required: true,
                  message: "必填项",
                },
              ]}
              name="lastFuelMeter"
            >
              <Input
                disabled={!isFirstMonthBill}
                placeholder="请输入燃气表记录值"
                type="number"
              ></Input>
            </Form.Item>
          )}
          <Form.Item
            label="当前月份燃气表记录值："
            rules={[
              {
                required: true,
                message: "必填项",
              },
            ]}
            name="fuelMeter"
          >
            <Input placeholder="请输入燃气表记录值" type="number"></Input>
          </Form.Item>
        </>
      )}
    </Form>
  );
};

export default forwardRef(BillForm);
